package com.pmenergy.control;

// Generated 15/08/2014 01:13:31 AM by Hibernate Tools 3.4.0.CR1

import java.math.BigDecimal;
import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.pmenergy.model.Empleado;

/**
 * Home object for domain model class Empleado.
 * @see com.pmenergy.model.Empleado
 * @author Hibernate Tools
 */
@Stateless
public class EmpleadoHome {

	private static final Log log = LogFactory.getLog(EmpleadoHome.class);

	@PersistenceContext
	private EntityManager entityManager;

	public int persist(Empleado transientInstance) {
		int result=1;
		log.debug("persisting Empleado instance");
		try {
			entityManager.persist(transientInstance);			
			log.debug("persist successful");
			return result;
		} catch (RuntimeException re) {
			log.error("persist failed", re);
			return -1;
		}
	}

	public void remove(Empleado persistentInstance) {
		log.debug("removing Empleado instance");
		try {
			entityManager.remove(persistentInstance);
			log.debug("remove successful");
		} catch (RuntimeException re) {
			log.error("remove failed", re);
			throw re;
		}
	}

	public Empleado merge(Empleado detachedInstance) {
		log.debug("merging Empleado instance");
		try {
			Empleado result = entityManager.merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public Empleado findById(BigDecimal id) {
		log.debug("getting Empleado instance with id: " + id);
		try {
			Empleado instance = entityManager.find(Empleado.class, id);
			log.debug("get successful");
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}
	
	public int removeById(BigDecimal id) {
		log.debug("removing Empleado instance with id: " + id);
		try {
			int deletedEmpleados = entityManager.createQuery("DELETE FROM Empleado c WHERE c.empleadoId = :id")
					.setParameter("id",id).executeUpdate();
			log.debug("get successful");
			return deletedEmpleados;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			return -1;
		}
	}
		public Empleado findByUser(String user) {
			log.debug("getting Empleado instance with user: " + user);
			try {
				
				Empleado instance = (Empleado) entityManager.createQuery("SELECT c FROM Empleado c WHERE c.usuario = :user")
									.setParameter("user",user).getSingleResult();
				log.debug("get successful");
				return instance;
			} 
			catch (NoResultException re){
				return null;
			}
			catch (RuntimeException re) {
				log.error("get failed", re);
				throw re;
			}	
		}
		
		public List<Empleado> findAllEmpleados() {
			log.debug("getting all Empleados instances " );
			try {
				
				List<Empleado> list = entityManager.createQuery("SELECT c FROM Empleado c ").getResultList();
				log.debug("get successful");
				return list;
			} 
			/*catch (NoResultException re){
				return null;
			}*/
			catch (RuntimeException re) {
				log.error("get failed", re);
				return null;
			}
		}
		
		public List<Empleado> findEmpleadosByFields(String nombre,String usuario,String departamento,String cargo,String estado) 
				throws RuntimeException{
			log.debug("getting all Empleados instances " );
			try {
				Query query;
				String q="SELECT c FROM Empleado c  WHERE "
						+  "c.nombre LIKE :nombre AND "
						+  "c.departamento LIKE :departamento AND "
						+  "c.cargo LIKE :cargo AND "
						+  "c.usuario LIKE :usuario";
				
				
				if (estado != null) q+=  " AND c.estado = :estado";
					
				query = entityManager.createQuery(q);	
						
				query.setParameter("nombre","%" + nombre + "%" );
				query.setParameter("departamento","%" + departamento + "%");
				query.setParameter("cargo","%" + cargo + "%");																	
				query.setParameter("usuario","%" + usuario + "%");
				
				if (estado != null) query.setParameter("estado",estado.charAt(0));
				
				List<Empleado> list = query.getResultList();
				log.debug("get successful");
				
				return list;
			} 
			/*catch (NoResultException re){
				return null;
			}*/
			catch (RuntimeException re) {
				log.error("get failed", re);
				return null;
				//throw re;
				
			}
		}
		
		public int updateEmpleado(BigDecimal empleadoId,String nombre,String telefono1,String telefono2,
				String email,String departamento,String cargo,String usuario,String clave,char estado) {
			
			int updatedEmpleado;
			log.debug("updating Empleado instance " );
			try {
				
				Query query;
				query = entityManager.createQuery("UPDATE Empleado c SET c.nombre = :nombre,"
																					+  "c.telefono1 = :telefono1,"
																					+  "c.telefono2 = :telefono2,"
																					+  "c.email = :email,"
																					+  "c.departamento = :departamento,"
																					+  "c.cargo = :cargo,"
																					+  "c.usuario = :usuario,"
																					+  "c.clave = :clave,"
																					+  "c.estado = :estado "
																					+ " WHERE c.empleadoId = :id");
				
																					
				query.setParameter("id",empleadoId);
				query.setParameter("nombre",nombre);
				query.setParameter("telefono1",telefono1);
				query.setParameter("telefono2",telefono2);
				query.setParameter("email",email);
				query.setParameter("departamento",departamento);
				query.setParameter("cargo",cargo);																	
				query.setParameter("usuario",usuario);
				query.setParameter("clave",clave);
				query.setParameter("estado",estado);
				
				updatedEmpleado = query.executeUpdate();
				return updatedEmpleado;
			}			
			catch (RuntimeException re) {
				log.error("get failed", re);				
				return -1;
				
			}
		}
}
